import { isString, every, includes } from 'lodash';

const util = {
    /**
     * 遍历数据，返回与回调（callback）结果合并的结果
     * @param {Array | Object} rows
     * @param {*} callback
     * @returns
     */
    mapRows(rows, callback) {
        if (!rows) {
            return rows;
        }

        if (Array.isArray(rows)) {
            return rows.map(row => ({ ...row, ...callback(row) }));
        }

        return { ...rows, ...callback(rows) };
    },
    /**
     * 利用setTimeout延迟执行
     * @param {Function} fn
     * @returns Function
     */
    delayExecute: fn => () => {
        setTimeout(fn, 50);
    },

    /**
     * 是否需要校验
     * @param validateFields
     * @param { * | Array}allValidateFields
     * @returns {boolean}
     */
    needValidate(validateFields, allValidateFields) {
        if (!allValidateFields || allValidateFields === '*') {
            return true;
        }

        if (isString(validateFields) && includes(allValidateFields, validateFields)) {
            return true;
        }

        return every(validateFields, field => includes(allValidateFields, field));
    }
};

export default util;
